layui.config({
    base: "../../js/"
}).use(['jquery', 'frame', 'table', 'form', 'http', 'config'], function () {
    var $table = layui.table,
        $ = layui.jquery,
        $form = layui.form,
        $frame = layui.frame(),
        $cfg = layui.config(),
        $http = layui.http();

    $table.render({
        elem: '#list'
        , url: $cfg.config.baseUrl + 'acp/music/ajaxIntegralTypeList.json'
        , where: {}

        , cols: [[ //标题栏
            {checkbox: true, LAY_CHECKED: false, fixed:"left"}
            , {field: 'type',width:300, fixed:"left", title: '类型', align: 'center', templet: function (d) {
                    if(d.type == 0) {
                        return "查询单词";
                    } else {
                        return "看广告";
                    }
                }}
            , {field: 'desc', title: '描述',width:150, fixed:"left", align: 'center'}
            , {field: 'integral',title: '积分', fixed:"left", align: 'center', width: 150}
            , {fixed:"right", title: '操作', width: 300, align: 'center', toolbar: '#barList'}
        ]]
        , response: {
            statusName: 'status' //数据状态的字段名称，默认：code
            , statusCode: 0 //成功的状态码，默认：0
            , msgName: 'msg' //状态信息的字段名称，默认：msg
            , countName: 'num' //数据总数的字段名称，默认：count
            , dataName: 'list' //数据列表的字段名称，默认：data
        }
        , skin: 'row' //表格风格
        , even: true
        , page: true //是否显示分页
        , limits: [10, 15, 20]
        , limit: 10 //每页默认显示的数量
        , id: "list"
    });

    $form.render();

    //新增
    $("#btn-add").click(function () {
        layer.open({
            type: 2,
            content: $cfg.config.baseUrl + 'acp/music/integraltypeEdit.html',
            area: ['450px', '400px'],
            title: ['新增', 'font-size:18px;'],
            end: function () {
                //关闭之后刷新加载
                location.reload();
            }
        })
    });

    $("#addAndEdit").click(function () {
        var id = $("#hideId").val();
        var desc = $("#desc").val();
        var integral = $("#integral").val();
        $.ajax({
            url: $cfg.config.baseUrl + "acp/music/ajaxIntegralTypeEdit.json",
            method:'post',
            data:{
                id: id,
                desc: desc,
                integral: integral
            },
            dataType:'JSON',
            async:false,
            success:function(data){
                if(data.status == 0) {
                    $frame.message("提交数据成功");
                    refresh();
                    $frame.close();
                } else {
                    $frame.message(data.msg);
                }
            },
            error:function (data) {
                console.log(data);
            }
        });
    });

    //刷新表格，重新查询
    $(".btn-select").click(function () {
        refresh();
    });

    //重置按钮
    $("#resetUserList").click(function () {
        // refresh();
        $table.reload("list", {
            where: {}
        })
    });
    
    /**
     * 刷新
     */
    function refresh() {
        var type = $("#integraltype").val();
        $table.reload("list", {
            where: {type: type}
        })
    }

    $table.on('tool(list)', function (obj) {
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        var id = data.id;
        if (layEvent === 'look') {
            //弹出层
            layer.open({
                type: 2,
                content: $cfg.config.baseUrl + 'acp/music/integraltypeEdit.html?id=' + id,
                area: ['450px', '400px'],
                title: ['修改', 'font-size:18px;'],
                end: function () {
                    //关闭之后刷新加载
                    location.reload();
                }
            })
        } else if (layEvent === 'del') {
            var url = "acp/music/ajaxintegraltypeDel.json?id=" + id;
            $frame.messageBox("您确定要<label style='color:red;'>[删除]</label>吗?", function (index, layero) {
                var data1 = $http.request(url, {id: id});
                if (data1) {
                    if (data1.status == 0) {
                        $frame.message("删除成功");
                        refresh();
                        $frame.closeWindow(index);
                    } else {
                        $frame.message(data1.msg);
                    }
                }
            });
        }
    });
});